package org.algorithm.二师兄.链表;

import org.algorithm.common.ListNode;

public class 第k个节点 {


    public static void main(String[] args) {
        ListNode listNode = ListNode.build(1,2,3,4,5);

       int k = kNode(listNode, 4);
        System.out.println("k = " + k);

    }

    private static int kNode(ListNode listNode, int k) {
        if (listNode == null) return -1;
        ListNode left = listNode;
        ListNode right = listNode;
        for (int i = 0; i < k; i++) {
            right = right.next;
        }
        while (right != null) {
            left = left.next;
            right = right.next;
        }
        return left.val;
    }


}
